package com.lightboat.web.controller.crm;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.page.TableDataInfo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.common.enums.RedisSnEnum;
import com.lightboat.common.utils.RedisSnGenerator;
import com.lightboat.common.utils.poi.ExcelUtil;
import com.lightboat.system.domain.CrmAfterSale;
import com.lightboat.system.domain.request.CrmAfterSaleQueryRequest;
import com.lightboat.system.service.ICrmAfterSaleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 售后Controller
 *
 * @author lightboat
 * @date 2025-09-09
 */
@Tag(name = "售后管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/sale")
public class CrmAfterSaleController extends BaseController {

    private final ICrmAfterSaleService crmAfterSaleService;

    /**
     * 查询售后列表
     */
    @Operation(summary = "查询售后列表")
    @PreAuthorize("@ss.hasPermi('customerService:afterSale:list')")
    @GetMapping("/list")
    public TableDataInfo<CrmAfterSale> list(CrmAfterSaleQueryRequest request) {
        startPage();
        CrmAfterSale crmAfterSale = BeanUtil.copyProperties(request, CrmAfterSale.class);
        List<CrmAfterSale> list = crmAfterSaleService.selectCrmAfterSaleList(crmAfterSale);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 导出售后列表
     */
    @Operation(summary = "导出售后列表")
    @PreAuthorize("@ss.hasPermi('system:sale:export')")
    @Log(title = "售后", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmAfterSaleQueryRequest request) {
        CrmAfterSale crmAfterSale = BeanUtil.copyProperties(request, CrmAfterSale.class);
        List<CrmAfterSale> list = crmAfterSaleService.selectCrmAfterSaleList(crmAfterSale);
        ExcelUtil<CrmAfterSale> util = new ExcelUtil<CrmAfterSale>(CrmAfterSale.class);
        util.exportExcel(response, list, "售后数据");
    }

    /**
     * 获取售后详细信息
     */
    @Operation(summary = "获取售后详细信息")
    @PreAuthorize("@ss.hasPermi('system:sale:query')")
    @GetMapping(value = "/{afterSaleId}")
    public AjaxResult getInfo(@PathVariable("afterSaleId") Long afterSaleId) {
        return success(crmAfterSaleService.selectCrmAfterSaleByAfterSaleId(afterSaleId));
    }

    @Operation(summary = "根据订单ID获取售后详细信息")
    @PreAuthorize("@ss.hasPermi('system:sale:query')")
    @GetMapping(value = "/order/{orderId}")
    public AjaxResult getInfoByOrderId(@PathVariable("orderId") Long orderId) {
        return success(crmAfterSaleService.getOne(Wrappers.<CrmAfterSale>lambdaQuery().eq(CrmAfterSale::getOrderId, orderId).last("limit 1")));
    }

    /**
     * 新增售后
     */
    @Operation(summary = "新增售后")
    @PreAuthorize("@ss.hasPermi('system:sale:add')")
    @Log(title = "售后", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmAfterSale crmAfterSale) {
        RedisSnGenerator.build().getBusSn(RedisSnEnum.AFTER_SALE_ORDER);
        return toAjax(crmAfterSaleService.insertCrmAfterSale(crmAfterSale));
    }

    /**
     * 修改售后
     */
    @Operation(summary = "修改售后")
    @PreAuthorize("@ss.hasPermi('system:sale:edit')")
    @Log(title = "售后", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmAfterSale crmAfterSale) {
        return toAjax(crmAfterSaleService.updateCrmAfterSale(crmAfterSale));
    }

    /**
     * 删除售后
     */
    @Operation(summary = "删除售后")
    @PreAuthorize("@ss.hasPermi('system:sale:remove')")
    @Log(title = "售后", businessType = BusinessType.DELETE)
    @DeleteMapping("/{afterSaleIds}")
    public AjaxResult remove(@PathVariable Long[] afterSaleIds) {
        return toAjax(crmAfterSaleService.deleteCrmAfterSaleByAfterSaleIds(afterSaleIds));
    }
}
